package com.elevator;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import data.setting;

public class sqlitedb {
	Connection mConnection = null; 
    Statement mStatement = null;; 
	public sqlitedb(String dbFile,int nMask)
	{
	    try {
	    	File file=new File("elevatordb");
	    	if (!file.exists()) {
				file.mkdir();
			}
			Class.forName("org.sqlite.JDBC");
			mConnection = DriverManager.getConnection("jdbc:sqlite:elevatordb\\"+dbFile); 
			mStatement = mConnection.createStatement(); 
			createTable();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

	}
	public void createTable()
	{
		try {
			boolean resultSet = mStatement.execute("create table if not exists person(personId int,fromlayer int,tolayer int)");
		    if (resultSet) { 
		        System.out.println("create person ok");
		    } 
		    
			resultSet = mStatement.execute("create table if not exists setting(name varchar(64),value varchar(64))");
		    if (resultSet) { 
		        System.out.println("create setting ok");
		    }  
		    
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

	}
	public void storeSettingDB(simulationSettingUI settingUI)
	{
		try {
			Statement stmt=mConnection.createStatement();
			String sqlString = "insert into setting values('%s','%s')";
			mStatement.execute("delete from setting");
			mStatement.execute(String.format(sqlString, "sEvelatorCount",""+setting.sEvelatorCount));
			mStatement.execute(String.format(sqlString, "sLayerCount",""+setting.sLayerCount));
			mStatement.execute(String.format(sqlString, "mPreTimeBox",settingUI.mPreTimeBox.getSelectedIndex()));
			mStatement.execute(String.format(sqlString, "mProbobilityBox",settingUI.mProbobilityBox.getSelectedIndex()));
			
			mStatement.execute("delete from person");
			sqlString = "insert into person values(%d,%d,%d)";
			for (int i = 0; i < settingUI.mToLayer.length; i++) {
				mStatement.execute(String.format(sqlString, i,settingUI.mFromLayer[i],settingUI.mToLayer[i]));
			}
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public void getData(simulationSettingUI settingUI)
	{
		try {
			ResultSet rs=mStatement.executeQuery("select* from setting");
			while (rs.next())
			{
				String strName = rs.getString("name");
				String strValue = rs.getString("value");
				if (strName.equals("sEvelatorCount")) {
					setting.sEvelatorCount = Integer.parseInt(strValue);
				}
				else if (strName.equals("sLayerCount")) {
					setting.sLayerCount = Integer.parseInt(strValue);
				}
				else if (strName.equals("mPreTimeBox")) {
					settingUI.mPreTimeBox.setSelectedIndex(Integer.parseInt(strValue));
				}
				else if (strName.equals("mProbobilityBox")) {
					settingUI.mProbobilityBox.setSelectedIndex(Integer.parseInt(strValue));
				}
			}
			
			rs=mStatement.executeQuery("select* from person");
			while (rs.next())
			{
				int nid = rs.getInt("personId");
				int nFromLayer = rs.getInt("fromlayer");
				int nToLayer = rs.getInt("tolayer");
				if (nid < settingUI.mToLayer.length) {
					settingUI.mToLayer[nid] = nToLayer;
				}
				if (nid < settingUI.mFromLayer.length) {
					settingUI.mFromLayer[nid] = nFromLayer;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}
	public void closeDB()
	{
	    try {
			mStatement.close();
			mConnection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	    
	}
}
